package com.example.bgmachine.bgmachine.produces;

import android.app.Dialog;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.TextView;

import com.example.bgmachine.bgmachine.R;
import com.example.bgmachine.bgmachine.homes.HomePages;

public class ProducePopupView  extends Dialog implements
        View.OnClickListener {
    Context context;


    private Boolean bool;
    private View.OnClickListener btnOnClick;
    public Button procduce_popup_cencel_btn = null;

    public TextView produce_popup_view_progress_textview = null;
    public TextView produce_popup_view_name_textview = null;
    public int currentTimes =0;
    public int allTimes =0;
    private Handler mTimeTestOne = new Handler() {
        public void handleMessage(android.os.Message msg) {
            mTimeTestOne.removeMessages(301);
            currentTimes ++;

            if(allTimes <= 0){
                allTimes = 1;
            }
            int numss=currentTimes * 100 /allTimes;
            if (numss>= 100){
                numss = 100;
            }
            setRunInfor(String.valueOf( numss ) );

            if(currentTimes < (allTimes + 5)){

                mTimeTestOne.sendEmptyMessageDelayed(301, 1000);

            }else{
                dismiss();
            }

        }
    };

    /**
     * @param @param btnOnClick
     * @return void
     * @throws
     * @Description: 可以自定义button事件
     * @author Sunday
     */
    public void setBtnOnClick(View.OnClickListener btnOnClick) {
        this.btnOnClick = btnOnClick;

    }

    public void setBtnOkClick(String text,
                              View.OnClickListener btnOnClick) {
        this.btnOnClick = btnOnClick;

    }

    public ProducePopupView(Context context) {
        super(context, R.style.SundayDialogs);
        this.context = context;
        bool = true;


        Log.d("aaa", "aaa===2");
        //create();
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.produce_view_select_dialog);

        hideBottomUIMenu();
        // 设置窗口属性
        if (getWindow() != null) {
            getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
            getWindow().setDimAmount(0f);
            getWindow().setFlags(
                    WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL,
                    WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL
            );
            getWindow().setFlags(
                    WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH,
                    WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH
            );
        }
        init();
        //  setCancel(bool, "");

        Log.d("aaa", "aaa===1");
    }
    /**
     * 隐藏虚拟按键，并且全屏
     */
    protected void hideBottomUIMenu() {
        WindowManager.LayoutParams params = getWindow().getAttributes();
        params.width = WindowManager.LayoutParams.MATCH_PARENT;
        params.height = WindowManager.LayoutParams.MATCH_PARENT;
        getWindow().setAttributes(params);


        int flag = WindowManager.LayoutParams.FLAG_FULLSCREEN;
        Window myWindow = this.getWindow();
        myWindow.setFlags(flag, flag);

        //隐藏虚拟按键，并且全屏
        if (Build.VERSION.SDK_INT > 11 && Build.VERSION.SDK_INT < 19) { // lower api
            View v = this.getWindow().getDecorView();
            v.setSystemUiVisibility(View.GONE);
        } else if (Build.VERSION.SDK_INT >= 19) {
            //for new api versions.
            View decorView = getWindow().getDecorView();
            int uiOptions = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
                    | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY | View.SYSTEM_UI_FLAG_FULLSCREEN;
            decorView.setSystemUiVisibility(uiOptions);


        }

        getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
                | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY | View.SYSTEM_UI_FLAG_FULLSCREEN);
        getWindow().getDecorView().setOnSystemUiVisibilityChangeListener(new View.OnSystemUiVisibilityChangeListener() {
            @Override
            public void onSystemUiVisibilityChange(int visibility) {
                getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
                        | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY | View.SYSTEM_UI_FLAG_FULLSCREEN);
            }
        });
    }
    void init() {
        procduce_popup_cencel_btn = findViewById(R.id.procduce_popup_cencel_btn);
        procduce_popup_cencel_btn.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                mTimeTestOne.removeMessages(301);
                HomePages.mHomePages.cancelOut();
                dismiss();
            }
        });

        produce_popup_view_progress_textview = findViewById(R.id.produce_popup_view_progress_textview);

        produce_popup_view_name_textview = findViewById(R.id.produce_popup_view_name_textview);

    }
    public void closeView(){
        mTimeTestOne.removeMessages(301);

        dismiss();
    }
    public void setNameInfor(String infors){
        if(produce_popup_view_name_textview != null){
            produce_popup_view_name_textview.setText(infors);
        }
    }
    public void setRunTime(int times){
        currentTimes =0;
        allTimes = times;
        setRunInfor("0");
        mTimeTestOne.sendEmptyMessageDelayed(301, 1000);

    }

    public void setRunInfor(String infors){
        if(produce_popup_view_progress_textview != null){
            produce_popup_view_progress_textview.setText(infors);
        }
    }
    /*
            *
            */
    @Override
    public void onClick(View arg0) {
        // TODO Auto-generated method stub
        if (isShowing()) {
            //   dismiss();
        }
    }
}
